package furns.web;

import furns.entity.Cart;
import furns.entity.Member;
import furns.entity.Order;
import furns.entity.OrderItem;
import furns.service.OrderService;
import furns.service.impl.OrderServiceImpl;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.WebServlet;
import java.io.IOException;
import java.util.List;

@SuppressWarnings("all")
@WebServlet(name = "OrderServlet", urlPatterns = "/orderServlet")
public class OrderServlet extends BasicServlet {

    private OrderService orderService = new OrderServiceImpl();

    /**
     * 生成订单
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void saveOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1、获取购物车对象
        Cart cart = (Cart) request.getSession().getAttribute("cart");
        //如果cart不存在或者为空，表示购物车中没有商品，则跳转到首页进行添加
        if (cart == null || cart.isEmpty()) {
            request.getRequestDispatcher("/index.jsp").forward(request, response);
            return;
        }
        //2、获取memberId
        Member member = (Member) request.getSession().getAttribute("member");
        if (member == null) { //表示用户未登录
            //跳转到登录页面
            request.getRequestDispatcher("/views/member/login.jsp").forward(request, response);
            return;
        }
        //3、当cart对象和member对象都不为null时，调用service层方法生成订单号
        String orderId = orderService.saveOrder(cart, member.getId());
        //因为后续是重定向，因此需要放入session中
        request.getSession().setAttribute("orderId", orderId);
        //重定向到checkout.jsp页面  重定向是发送新的请求，因此需要使用完整的路径
        response.sendRedirect(request.getContextPath() + "/views/order/checkout.jsp");
    }

    /**
     * 查询总订单
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void queryOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1、获取memberId
        Member member = (Member) request.getSession().getAttribute("member");
        if (member == null) { //表示用户未登录
            //跳转到登录页面
            request.getRequestDispatcher("/views/member/login.jsp").forward(request, response);
            return;
        }
        //2、根据memberId查询订单项
        List<Order> orders = orderService.queryOrderByMemberId(member.getId());
        //3、将订单项放入session域中
        request.getSession().setAttribute("orders", orders);
        //4、跳转到订单页面
        request.getRequestDispatcher("/views/order/order.jsp").forward(request, response);
    }

    /**
     * 查询总订单中的订单项
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void queryOrderItem(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取订单号
        String orderId = request.getParameter("orderId");
        //遍历每个订单取出其中的订单项
        List<OrderItem> orderItems = orderService.queryOrderItemByOrderId(orderId);
        //将订单项放入session域中
        request.getSession().setAttribute("orderItems", orderItems);
        //跳转到订单详情页面
        request.getRequestDispatcher("/views/order/order_detail.jsp").forward(request, response);
    }
}